{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "16638a81",
   "metadata": {},
   "source": [
    "准备工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "d5dda0ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 数据处理核心库\n",
    "import pandas as pd  # 数据处理核心库\n",
    "from IPython.core.interactiveshell import InteractiveShell  # 设置Jupyter输出\n",
    "\n",
    "# 设置Jupyter输出每个表达式的结果\n",
    "InteractiveShell.ast_node_interactivity = 'all'\n",
    "\n",
    "# 设置显示选项（可取消注释以启用）\n",
    "# pd.set_option('display.max_rows', None)    # 显示所有行\n",
    "# pd.set_option('display.max_columns', None) # 显示所有列\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2556664d",
   "metadata": {},
   "source": [
    "数据导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "6714eaba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 46.9 ms\n",
      "Wall time: 19.9 ms\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1990-12-19</th>\n",
       "      <td></td>\n",
       "      <td>96.050</td>\n",
       "      <td>99.980</td>\n",
       "      <td>95.790</td>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-20</th>\n",
       "      <td>99.98</td>\n",
       "      <td>104.300</td>\n",
       "      <td>104.390</td>\n",
       "      <td>99.980</td>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-21</th>\n",
       "      <td>104.39</td>\n",
       "      <td>109.070</td>\n",
       "      <td>109.130</td>\n",
       "      <td>103.730</td>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-24</th>\n",
       "      <td>109.13</td>\n",
       "      <td>113.570</td>\n",
       "      <td>114.550</td>\n",
       "      <td>109.130</td>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-25</th>\n",
       "      <td>114.55</td>\n",
       "      <td>120.090</td>\n",
       "      <td>120.250</td>\n",
       "      <td>114.550</td>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.35</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close\n",
       "Day                                                         \n",
       "1990-12-19              96.050    99.980    95.790    99.980\n",
       "1990-12-20     99.98   104.300   104.390    99.980   104.390\n",
       "1990-12-21    104.39   109.070   109.130   103.730   109.130\n",
       "1990-12-24    109.13   113.570   114.550   109.130   114.550\n",
       "1990-12-25    114.55   120.090   120.250   114.550   120.250\n",
       "...              ...       ...       ...       ...       ...\n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "2025-08-28   3800.35  3796.711  3845.087  3761.422  3843.597\n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[8473 rows x 5 columns]"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "data = pd.read_csv('000001.csv')\n",
    "data['Day'] = pd.to_datetime(data['Day'],format='%Y/%m/%d')\n",
    "data.set_index('Day', inplace = True)\n",
    "data.sort_values(by = ['Day'],axis=0, ascending=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "db8b9626",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.870</td>\n",
       "      <td>637.720</td>\n",
       "      <td>647.710</td>\n",
       "      <td>630.530</td>\n",
       "      <td>639.880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.880</td>\n",
       "      <td>641.900</td>\n",
       "      <td>655.510</td>\n",
       "      <td>638.860</td>\n",
       "      <td>653.810</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.810</td>\n",
       "      <td>655.380</td>\n",
       "      <td>657.520</td>\n",
       "      <td>645.810</td>\n",
       "      <td>646.890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.890</td>\n",
       "      <td>642.750</td>\n",
       "      <td>643.890</td>\n",
       "      <td>636.330</td>\n",
       "      <td>640.760</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.760</td>\n",
       "      <td>637.520</td>\n",
       "      <td>637.550</td>\n",
       "      <td>625.040</td>\n",
       "      <td>626.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7445 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close\n",
       "Day                                                         \n",
       "1995-01-03   647.870   637.720   647.710   630.530   639.880\n",
       "1995-01-04   639.880   641.900   655.510   638.860   653.810\n",
       "1995-01-05   653.810   655.380   657.520   645.810   646.890\n",
       "1995-01-06   646.890   642.750   643.890   636.330   640.760\n",
       "1995-01-09   640.760   637.520   637.550   625.040   626.000\n",
       "...              ...       ...       ...       ...       ...\n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597\n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[7445 rows x 5 columns]"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new = data['1995-01':'2025-08'].copy()\n",
    "data_new['Close'] = pd.to_numeric(data_new['Close'])\n",
    "data_new['Preclose'] = pd.to_numeric(data_new['Preclose'])\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9749319a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Loop_return</th>\n",
       "      <th>Loop_return2</th>\n",
       "      <th>Numpy_return</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Diff_div_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.870</td>\n",
       "      <td>637.720</td>\n",
       "      <td>647.710</td>\n",
       "      <td>630.530</td>\n",
       "      <td>639.880</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012409</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.880</td>\n",
       "      <td>641.900</td>\n",
       "      <td>655.510</td>\n",
       "      <td>638.860</td>\n",
       "      <td>653.810</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021536</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.810</td>\n",
       "      <td>655.380</td>\n",
       "      <td>657.520</td>\n",
       "      <td>645.810</td>\n",
       "      <td>646.890</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010641</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.890</td>\n",
       "      <td>642.750</td>\n",
       "      <td>643.890</td>\n",
       "      <td>636.330</td>\n",
       "      <td>640.760</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009521</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.760</td>\n",
       "      <td>637.520</td>\n",
       "      <td>637.550</td>\n",
       "      <td>625.040</td>\n",
       "      <td>626.000</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023305</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.014996</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003916</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017743</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011315</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003721</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7445 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Loop_return  \\\n",
       "Day                                                                         \n",
       "1995-01-03   647.870   637.720   647.710   630.530   639.880    -0.012333   \n",
       "1995-01-04   639.880   641.900   655.510   638.860   653.810     0.021770   \n",
       "1995-01-05   653.810   655.380   657.520   645.810   646.890    -0.010584   \n",
       "1995-01-06   646.890   642.750   643.890   636.330   640.760    -0.009476   \n",
       "1995-01-09   640.760   637.520   637.550   625.040   626.000    -0.023035   \n",
       "...              ...       ...       ...       ...       ...          ...   \n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562     0.015109   \n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382    -0.003909   \n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350    -0.017587   \n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597     0.011380   \n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927     0.003728   \n",
       "\n",
       "            Loop_return2  Numpy_return  Raw_return  Log_return  \\\n",
       "Day                                                              \n",
       "1995-01-03     -0.012333     -0.012333   -0.012333   -0.012409   \n",
       "1995-01-04      0.021770      0.021770    0.021770    0.021536   \n",
       "1995-01-05     -0.010584     -0.010584   -0.010584   -0.010641   \n",
       "1995-01-06     -0.009476     -0.009476   -0.009476   -0.009521   \n",
       "1995-01-09     -0.023035     -0.023035   -0.023035   -0.023305   \n",
       "...                  ...           ...         ...         ...   \n",
       "2025-08-25      0.015109      0.015109    0.015109    0.014996   \n",
       "2025-08-26     -0.003909     -0.003909   -0.003909   -0.003916   \n",
       "2025-08-27     -0.017587     -0.017587   -0.017587   -0.017743   \n",
       "2025-08-28      0.011380      0.011380    0.011380    0.011315   \n",
       "2025-08-29      0.003728      0.003728    0.003728    0.003721   \n",
       "\n",
       "            Pct_change_return  Apply_return  Diff_div_return  \n",
       "Day                                                           \n",
       "1995-01-03                NaN     -0.012333              NaN  \n",
       "1995-01-04           0.021770      0.021770         0.021770  \n",
       "1995-01-05          -0.010584     -0.010584        -0.010584  \n",
       "1995-01-06          -0.009476     -0.009476        -0.009476  \n",
       "1995-01-09          -0.023035     -0.023035        -0.023035  \n",
       "...                       ...           ...              ...  \n",
       "2025-08-25           0.015109      0.015109         0.015109  \n",
       "2025-08-26          -0.003909     -0.003909        -0.003909  \n",
       "2025-08-27          -0.017587     -0.017587        -0.017587  \n",
       "2025-08-28           0.011380      0.011380         0.011380  \n",
       "2025-08-29           0.003728      0.003728         0.003728  \n",
       "\n",
       "[7445 rows x 13 columns]"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算000001上证指数日收益率 - 方法1：直接使用向量化操作（最推荐的方式）\n",
    "data_new['Raw_return'] = data_new['Close'] / data_new['Preclose'] - 1\n",
    "data_new['Log_return'] = np.log(data_new['Close']) - np.log(data_new['Preclose'])\n",
    "\n",
    "# 方法2：使用pandas的pct_change函数计算收益率（适用于时间序列数据）\n",
    "# 注意：这种方法需要数据已经按时间排序\n",
    "data_new['Pct_change_return'] = data_new['Close'].pct_change()\n",
    "\n",
    "# 方法3：使用diff和div方法组合（另一种向量化操作）\n",
    "data_new['Diff_div_return'] = data_new['Close'].diff() / data_new['Close'].shift(1)\n",
    "\n",
    "# 比较不同方法计算结果的差异\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "3e96a064",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Loop_return</th>\n",
       "      <th>Loop_return2</th>\n",
       "      <th>Numpy_return</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Diff_div_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.870</td>\n",
       "      <td>637.720</td>\n",
       "      <td>647.710</td>\n",
       "      <td>630.530</td>\n",
       "      <td>639.880</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012409</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.880</td>\n",
       "      <td>641.900</td>\n",
       "      <td>655.510</td>\n",
       "      <td>638.860</td>\n",
       "      <td>653.810</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021536</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.810</td>\n",
       "      <td>655.380</td>\n",
       "      <td>657.520</td>\n",
       "      <td>645.810</td>\n",
       "      <td>646.890</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010641</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.890</td>\n",
       "      <td>642.750</td>\n",
       "      <td>643.890</td>\n",
       "      <td>636.330</td>\n",
       "      <td>640.760</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009521</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.760</td>\n",
       "      <td>637.520</td>\n",
       "      <td>637.550</td>\n",
       "      <td>625.040</td>\n",
       "      <td>626.000</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023305</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.014996</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003916</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017743</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011315</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003721</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7445 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Loop_return  \\\n",
       "Day                                                                         \n",
       "1995-01-03   647.870   637.720   647.710   630.530   639.880    -0.012333   \n",
       "1995-01-04   639.880   641.900   655.510   638.860   653.810     0.021770   \n",
       "1995-01-05   653.810   655.380   657.520   645.810   646.890    -0.010584   \n",
       "1995-01-06   646.890   642.750   643.890   636.330   640.760    -0.009476   \n",
       "1995-01-09   640.760   637.520   637.550   625.040   626.000    -0.023035   \n",
       "...              ...       ...       ...       ...       ...          ...   \n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562     0.015109   \n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382    -0.003909   \n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350    -0.017587   \n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597     0.011380   \n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927     0.003728   \n",
       "\n",
       "            Loop_return2  Numpy_return  Raw_return  Log_return  \\\n",
       "Day                                                              \n",
       "1995-01-03     -0.012333     -0.012333   -0.012333   -0.012409   \n",
       "1995-01-04      0.021770      0.021770    0.021770    0.021536   \n",
       "1995-01-05     -0.010584     -0.010584   -0.010584   -0.010641   \n",
       "1995-01-06     -0.009476     -0.009476   -0.009476   -0.009521   \n",
       "1995-01-09     -0.023035     -0.023035   -0.023035   -0.023305   \n",
       "...                  ...           ...         ...         ...   \n",
       "2025-08-25      0.015109      0.015109    0.015109    0.014996   \n",
       "2025-08-26     -0.003909     -0.003909   -0.003909   -0.003916   \n",
       "2025-08-27     -0.017587     -0.017587   -0.017587   -0.017743   \n",
       "2025-08-28      0.011380      0.011380    0.011380    0.011315   \n",
       "2025-08-29      0.003728      0.003728    0.003728    0.003721   \n",
       "\n",
       "            Pct_change_return  Apply_return  Diff_div_return  \n",
       "Day                                                           \n",
       "1995-01-03                NaN     -0.012333              NaN  \n",
       "1995-01-04           0.021770      0.021770         0.021770  \n",
       "1995-01-05          -0.010584     -0.010584        -0.010584  \n",
       "1995-01-06          -0.009476     -0.009476        -0.009476  \n",
       "1995-01-09          -0.023035     -0.023035        -0.023035  \n",
       "...                       ...           ...              ...  \n",
       "2025-08-25           0.015109      0.015109         0.015109  \n",
       "2025-08-26          -0.003909     -0.003909        -0.003909  \n",
       "2025-08-27          -0.017587     -0.017587        -0.017587  \n",
       "2025-08-28           0.011380      0.011380         0.011380  \n",
       "2025-08-29           0.003728      0.003728         0.003728  \n",
       "\n",
       "[7445 rows x 13 columns]"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法7：使用numpy的向量化操作（高效且简洁）\n",
    "data_new['Numpy_return'] = (data_new['Close'].values / data_new['Preclose'].values) - 1\n",
    "\n",
    "# 显示结果\n",
    "data_new"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc3b3086",
   "metadata": {},
   "source": [
    "月度收益"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "87d22a9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "      <th>Year</th>\n",
       "      <th>Month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-31</th>\n",
       "      <td>-0.141139</td>\n",
       "      <td>-0.131631</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-02-28</th>\n",
       "      <td>-0.023979</td>\n",
       "      <td>-0.023694</td>\n",
       "      <td>1995</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>0.163651</td>\n",
       "      <td>0.177803</td>\n",
       "      <td>1995</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-04-30</th>\n",
       "      <td>-0.109315</td>\n",
       "      <td>-0.103552</td>\n",
       "      <td>1995</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-31</th>\n",
       "      <td>0.188901</td>\n",
       "      <td>0.207922</td>\n",
       "      <td>1995</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return  Year  Month\n",
       "Day                                            \n",
       "1995-01-31   -0.141139   -0.131631  1995      1\n",
       "1995-02-28   -0.023979   -0.023694  1995      2\n",
       "1995-03-31    0.163651    0.177803  1995      3\n",
       "1995-04-30   -0.109315   -0.103552  1995      4\n",
       "1995-05-31    0.188901    0.207922  1995      5"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法1：使用resample函数计算月度对数收益率并转换为原始收益率\n",
    "# 这种方法适合对数收益率，因为对数收益率可以直接相加\n",
    "Month_data1 = data_new.resample('ME')['Log_return'].sum().to_frame(name='Log_return') \n",
    "Month_data1['Raw_Return'] = np.exp(Month_data1['Log_return']) - 1\n",
    "\n",
    "# 添加年月信息便于分析\n",
    "Month_data1['Year'] = Month_data1.index.year\n",
    "Month_data1['Month'] = Month_data1.index.month\n",
    "\n",
    "# 显示结果\n",
    "Month_data1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "392f9214",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Year</th>\n",
       "      <th>Month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-31</th>\n",
       "      <td>562.59</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-02-28</th>\n",
       "      <td>549.26</td>\n",
       "      <td>562.59</td>\n",
       "      <td>-0.023694</td>\n",
       "      <td>-0.023979</td>\n",
       "      <td>1995</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>646.92</td>\n",
       "      <td>549.26</td>\n",
       "      <td>0.177803</td>\n",
       "      <td>0.163651</td>\n",
       "      <td>1995</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-04-30</th>\n",
       "      <td>579.93</td>\n",
       "      <td>646.92</td>\n",
       "      <td>-0.103552</td>\n",
       "      <td>-0.109315</td>\n",
       "      <td>1995</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-31</th>\n",
       "      <td>700.51</td>\n",
       "      <td>579.93</td>\n",
       "      <td>0.207922</td>\n",
       "      <td>0.188901</td>\n",
       "      <td>1995</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Close  Preclose  Raw_return  Log_return  Year  Month\n",
       "Day                                                              \n",
       "1995-01-31  562.59       NaN         NaN         NaN  1995      1\n",
       "1995-02-28  549.26    562.59   -0.023694   -0.023979  1995      2\n",
       "1995-03-31  646.92    549.26    0.177803    0.163651  1995      3\n",
       "1995-04-30  579.93    646.92   -0.103552   -0.109315  1995      4\n",
       "1995-05-31  700.51    579.93    0.207922    0.188901  1995      5"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法2：使用resample取月末价格计算月度收益率\n",
    "# 这种方法直接使用月末价格计算收益率，更符合金融实践\n",
    "Month_data2 = data_new.resample('ME')['Close'].last().to_frame()\n",
    "Month_data2['Preclose'] = Month_data2['Close'].shift(1)\n",
    "Month_data2['Raw_return'] = Month_data2['Close'] / Month_data2['Preclose'] - 1\n",
    "Month_data2['Log_return'] = np.log(Month_data2['Close']) - np.log(Month_data2['Preclose'])\n",
    "\n",
    "# 添加年月信息\n",
    "Month_data2['Year'] = Month_data2.index.year\n",
    "Month_data2['Month'] = Month_data2.index.month\n",
    "\n",
    "# 显示结果\n",
    "Month_data2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "d6d0b4b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Loop_return</th>\n",
       "      <th>Loop_return2</th>\n",
       "      <th>Numpy_return</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Diff_div_return</th>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.870</td>\n",
       "      <td>637.720</td>\n",
       "      <td>647.710</td>\n",
       "      <td>630.530</td>\n",
       "      <td>639.880</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012409</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.880</td>\n",
       "      <td>641.900</td>\n",
       "      <td>655.510</td>\n",
       "      <td>638.860</td>\n",
       "      <td>653.810</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021536</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.810</td>\n",
       "      <td>655.380</td>\n",
       "      <td>657.520</td>\n",
       "      <td>645.810</td>\n",
       "      <td>646.890</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010641</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.890</td>\n",
       "      <td>642.750</td>\n",
       "      <td>643.890</td>\n",
       "      <td>636.330</td>\n",
       "      <td>640.760</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009521</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.760</td>\n",
       "      <td>637.520</td>\n",
       "      <td>637.550</td>\n",
       "      <td>625.040</td>\n",
       "      <td>626.000</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023305</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.014996</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003916</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017743</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011315</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003721</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7445 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Loop_return  \\\n",
       "Day                                                                         \n",
       "1995-01-03   647.870   637.720   647.710   630.530   639.880    -0.012333   \n",
       "1995-01-04   639.880   641.900   655.510   638.860   653.810     0.021770   \n",
       "1995-01-05   653.810   655.380   657.520   645.810   646.890    -0.010584   \n",
       "1995-01-06   646.890   642.750   643.890   636.330   640.760    -0.009476   \n",
       "1995-01-09   640.760   637.520   637.550   625.040   626.000    -0.023035   \n",
       "...              ...       ...       ...       ...       ...          ...   \n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562     0.015109   \n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382    -0.003909   \n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350    -0.017587   \n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597     0.011380   \n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927     0.003728   \n",
       "\n",
       "            Loop_return2  Numpy_return  Raw_return  Log_return  \\\n",
       "Day                                                              \n",
       "1995-01-03     -0.012333     -0.012333   -0.012333   -0.012409   \n",
       "1995-01-04      0.021770      0.021770    0.021770    0.021536   \n",
       "1995-01-05     -0.010584     -0.010584   -0.010584   -0.010641   \n",
       "1995-01-06     -0.009476     -0.009476   -0.009476   -0.009521   \n",
       "1995-01-09     -0.023035     -0.023035   -0.023035   -0.023305   \n",
       "...                  ...           ...         ...         ...   \n",
       "2025-08-25      0.015109      0.015109    0.015109    0.014996   \n",
       "2025-08-26     -0.003909     -0.003909   -0.003909   -0.003916   \n",
       "2025-08-27     -0.017587     -0.017587   -0.017587   -0.017743   \n",
       "2025-08-28      0.011380      0.011380    0.011380    0.011315   \n",
       "2025-08-29      0.003728      0.003728    0.003728    0.003721   \n",
       "\n",
       "            Pct_change_return  Apply_return  Diff_div_return  year  month  \n",
       "Day                                                                        \n",
       "1995-01-03                NaN     -0.012333              NaN  1995      1  \n",
       "1995-01-04           0.021770      0.021770         0.021770  1995      1  \n",
       "1995-01-05          -0.010584     -0.010584        -0.010584  1995      1  \n",
       "1995-01-06          -0.009476     -0.009476        -0.009476  1995      1  \n",
       "1995-01-09          -0.023035     -0.023035        -0.023035  1995      1  \n",
       "...                       ...           ...              ...   ...    ...  \n",
       "2025-08-25           0.015109      0.015109         0.015109  2025      8  \n",
       "2025-08-26          -0.003909     -0.003909        -0.003909  2025      8  \n",
       "2025-08-27          -0.017587     -0.017587        -0.017587  2025      8  \n",
       "2025-08-28           0.011380      0.011380         0.011380  2025      8  \n",
       "2025-08-29           0.003728      0.003728         0.003728  2025      8  \n",
       "\n",
       "[7445 rows x 15 columns]"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# “1996-11-13”日期格式 里面的year年份 month月份 day 直接提出取来\n",
    "data_new2 = data_new.copy()\n",
    "data_new2['year'] = data_new2.index.year\n",
    "data_new2['month'] = data_new2.index.month\n",
    "data_new2\n",
    "# 使用的时间、日期格式提取 字符串提出的方式 前四个字符当作年份 6-7字符是月份 提取出来的是字符串 变成数值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "dc01aa33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">1995</th>\n",
       "      <th>1</th>\n",
       "      <td>-0.141139</td>\n",
       "      <td>-0.131631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.023979</td>\n",
       "      <td>-0.023694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.163651</td>\n",
       "      <td>0.177803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.109315</td>\n",
       "      <td>-0.103552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.188901</td>\n",
       "      <td>0.207922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2025</th>\n",
       "      <th>4</th>\n",
       "      <td>-0.017148</td>\n",
       "      <td>-0.017002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.020662</td>\n",
       "      <td>0.020877</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.028547</td>\n",
       "      <td>0.028959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.036707</td>\n",
       "      <td>0.037389</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.076666</td>\n",
       "      <td>0.079682</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>368 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return\n",
       "year month                        \n",
       "1995 1       -0.141139   -0.131631\n",
       "     2       -0.023979   -0.023694\n",
       "     3        0.163651    0.177803\n",
       "     4       -0.109315   -0.103552\n",
       "     5        0.188901    0.207922\n",
       "...                ...         ...\n",
       "2025 4       -0.017148   -0.017002\n",
       "     5        0.020662    0.020877\n",
       "     6        0.028547    0.028959\n",
       "     7        0.036707    0.037389\n",
       "     8        0.076666    0.079682\n",
       "\n",
       "[368 rows x 2 columns]"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法3：使用groupby函数按年月分组计算月度收益率\n",
    "# 首先提取年月信息\n",
    "data_new3 = data_new.copy()\n",
    "data_new3['year'] = data_new3.index.year\n",
    "data_new3['month'] = data_new3.index.month\n",
    "\n",
    "# 使用groupby按年月分组，然后对每组的对数收益率求和\n",
    "Month_data3 = data_new3.groupby(['year', 'month'])['Log_return'].sum().to_frame()\n",
    "Month_data3['Raw_Return'] = np.exp(Month_data3['Log_return']) - 1\n",
    "\n",
    "# 显示结果\n",
    "Month_data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "f0deea59",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法4结果:\n",
      "            Log_return  Raw_Return\n",
      "year month                        \n",
      "1995 1       -0.141139   -0.131631\n",
      "     2       -0.023979   -0.023694\n",
      "     3        0.163651    0.177803\n",
      "     4       -0.109315   -0.103552\n",
      "     5        0.188901    0.207922\n",
      "\n",
      "方法5结果 (包含多个统计量):\n",
      "           Log_return                           Raw_return          \n",
      "                  sum      mean       std count       mean       std\n",
      "year month                                                          \n",
      "1995 1      -0.141139 -0.007428  0.016251    19  -0.007277  0.016140\n",
      "     2      -0.023979 -0.001411  0.033003    17  -0.000891  0.033609\n",
      "     3       0.163651  0.007115  0.023204    23   0.007401  0.023470\n",
      "     4      -0.109315 -0.005466  0.020374    20  -0.005255  0.020169\n",
      "     5       0.188901  0.008586  0.077844    22   0.011645  0.083211\n"
     ]
    }
   ],
   "source": [
    "# 方法4：使用apply和lambda函数进行更灵活的分组计算\n",
    "# 这种方法可以对每个月的数据进行更复杂的操作\n",
    "Month_data4 = pd.DataFrame(\n",
    "    data_new3.groupby(['year', 'month'])['Log_return'].apply(lambda x: sum(x)))\n",
    "Month_data4.columns = ['Log_return']\n",
    "Month_data4['Raw_Return'] = np.exp(Month_data4['Log_return']) - 1\n",
    "\n",
    "# 方法5：使用agg函数同时计算多个统计量\n",
    "Month_data5 = data_new3.groupby(['year', 'month']).agg({\n",
    "    'Log_return': ['sum', 'mean', 'std', 'count'],\n",
    "    'Raw_return': ['mean', 'std']\n",
    "})\n",
    "\n",
    "# 显示结果\n",
    "print(\"方法4结果:\")\n",
    "print(Month_data4.head())\n",
    "print(\"\\n方法5结果 (包含多个统计量):\")\n",
    "print(Month_data5.head())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2a5fb4f",
   "metadata": {},
   "source": [
    "季度收益"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "932e2b32",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "季度对数收益率汇总:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "      <th>Year</th>\n",
       "      <th>Quarter</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>-0.001467</td>\n",
       "      <td>-0.001466</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-06-30</th>\n",
       "      <td>-0.025583</td>\n",
       "      <td>-0.025258</td>\n",
       "      <td>1995</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-09-30</th>\n",
       "      <td>0.135980</td>\n",
       "      <td>0.145660</td>\n",
       "      <td>1995</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>-0.263130</td>\n",
       "      <td>-0.231358</td>\n",
       "      <td>1995</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-03-31</th>\n",
       "      <td>0.001979</td>\n",
       "      <td>0.001981</td>\n",
       "      <td>1996</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-30</th>\n",
       "      <td>0.117234</td>\n",
       "      <td>0.124383</td>\n",
       "      <td>2024</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>0.004565</td>\n",
       "      <td>0.004575</td>\n",
       "      <td>2024</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-03-31</th>\n",
       "      <td>-0.004790</td>\n",
       "      <td>-0.004779</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-06-30</th>\n",
       "      <td>0.032061</td>\n",
       "      <td>0.032580</td>\n",
       "      <td>2025</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-09-30</th>\n",
       "      <td>0.113373</td>\n",
       "      <td>0.120049</td>\n",
       "      <td>2025</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>123 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return  Year  Quarter\n",
       "Day                                              \n",
       "1995-03-31   -0.001467   -0.001466  1995        1\n",
       "1995-06-30   -0.025583   -0.025258  1995        2\n",
       "1995-09-30    0.135980    0.145660  1995        3\n",
       "1995-12-31   -0.263130   -0.231358  1995        4\n",
       "1996-03-31    0.001979    0.001981  1996        1\n",
       "...                ...         ...   ...      ...\n",
       "2024-09-30    0.117234    0.124383  2024        3\n",
       "2024-12-31    0.004565    0.004575  2024        4\n",
       "2025-03-31   -0.004790   -0.004779  2025        1\n",
       "2025-06-30    0.032061    0.032580  2025        2\n",
       "2025-09-30    0.113373    0.120049  2025        3\n",
       "\n",
       "[123 rows x 4 columns]"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "季度末价格计算的收益率:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>646.920</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-06-30</th>\n",
       "      <td>630.580</td>\n",
       "      <td>646.9200</td>\n",
       "      <td>-0.025258</td>\n",
       "      <td>-0.025583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-09-30</th>\n",
       "      <td>722.430</td>\n",
       "      <td>630.5800</td>\n",
       "      <td>0.145660</td>\n",
       "      <td>0.135980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>555.290</td>\n",
       "      <td>722.4300</td>\n",
       "      <td>-0.231358</td>\n",
       "      <td>-0.263130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-03-31</th>\n",
       "      <td>556.390</td>\n",
       "      <td>555.2900</td>\n",
       "      <td>0.001981</td>\n",
       "      <td>0.001979</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-30</th>\n",
       "      <td>3336.497</td>\n",
       "      <td>2967.4028</td>\n",
       "      <td>0.124383</td>\n",
       "      <td>0.117234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>3351.763</td>\n",
       "      <td>3336.4970</td>\n",
       "      <td>0.004575</td>\n",
       "      <td>0.004565</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-03-31</th>\n",
       "      <td>3335.746</td>\n",
       "      <td>3351.7630</td>\n",
       "      <td>-0.004779</td>\n",
       "      <td>-0.004790</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-06-30</th>\n",
       "      <td>3444.426</td>\n",
       "      <td>3335.7460</td>\n",
       "      <td>0.032580</td>\n",
       "      <td>0.032061</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-09-30</th>\n",
       "      <td>3857.927</td>\n",
       "      <td>3444.4260</td>\n",
       "      <td>0.120049</td>\n",
       "      <td>0.113373</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>123 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               Close   Preclose  Raw_return  Log_return\n",
       "Day                                                    \n",
       "1995-03-31   646.920        NaN         NaN         NaN\n",
       "1995-06-30   630.580   646.9200   -0.025258   -0.025583\n",
       "1995-09-30   722.430   630.5800    0.145660    0.135980\n",
       "1995-12-31   555.290   722.4300   -0.231358   -0.263130\n",
       "1996-03-31   556.390   555.2900    0.001981    0.001979\n",
       "...              ...        ...         ...         ...\n",
       "2024-09-30  3336.497  2967.4028    0.124383    0.117234\n",
       "2024-12-31  3351.763  3336.4970    0.004575    0.004565\n",
       "2025-03-31  3335.746  3351.7630   -0.004779   -0.004790\n",
       "2025-06-30  3444.426  3335.7460    0.032580    0.032061\n",
       "2025-09-30  3857.927  3444.4260    0.120049    0.113373\n",
       "\n",
       "[123 rows x 4 columns]"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算季度收益率\n",
    "# 方法1：使用resample函数的'QE'参数（季度末）\n",
    "Quarter_data1 = data_new.resample('QE')['Log_return'].sum().to_frame(name='Log_return')\n",
    "Quarter_data1['Raw_Return'] = np.exp(Quarter_data1['Log_return']) - 1\n",
    "Quarter_data1['Year'] = Quarter_data1.index.year\n",
    "Quarter_data1['Quarter'] = Quarter_data1.index.quarter\n",
    "\n",
    "# 方法2：使用季度末价格计算\n",
    "Quarter_data2 = data_new.resample('QE')['Close'].last().to_frame()\n",
    "Quarter_data2['Preclose'] = Quarter_data2['Close'].shift(1)\n",
    "Quarter_data2['Raw_return'] = Quarter_data2['Close'] / Quarter_data2['Preclose'] - 1\n",
    "Quarter_data2['Log_return'] = np.log(Quarter_data2['Close']) - np.log(Quarter_data2['Preclose'])\n",
    "\n",
    "# 显示结果\n",
    "print(\"季度对数收益率汇总:\")\n",
    "Quarter_data1\n",
    "print(\"\\n季度末价格计算的收益率:\")\n",
    "Quarter_data2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ded999dd",
   "metadata": {},
   "source": [
    "年度收益"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "836954a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年度对数收益率汇总:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>-0.154200</td>\n",
       "      <td>-0.142899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-12-31</th>\n",
       "      <td>0.501639</td>\n",
       "      <td>0.651425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-12-31</th>\n",
       "      <td>0.264019</td>\n",
       "      <td>0.302153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-12-31</th>\n",
       "      <td>-0.040505</td>\n",
       "      <td>-0.039695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1999-12-31</th>\n",
       "      <td>0.175423</td>\n",
       "      <td>0.191750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-12-31</th>\n",
       "      <td>0.416917</td>\n",
       "      <td>0.517277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-12-31</th>\n",
       "      <td>-0.230898</td>\n",
       "      <td>-0.206180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>-0.192575</td>\n",
       "      <td>-0.175167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-12-31</th>\n",
       "      <td>0.097735</td>\n",
       "      <td>0.102670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-12-31</th>\n",
       "      <td>-0.167233</td>\n",
       "      <td>-0.153997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-12-31</th>\n",
       "      <td>-0.086924</td>\n",
       "      <td>-0.083253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-12-31</th>\n",
       "      <td>0.834792</td>\n",
       "      <td>1.304334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-12-31</th>\n",
       "      <td>0.676302</td>\n",
       "      <td>0.966593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-12-31</th>\n",
       "      <td>-1.061146</td>\n",
       "      <td>-0.653941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-12-31</th>\n",
       "      <td>0.587690</td>\n",
       "      <td>0.799825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-12-31</th>\n",
       "      <td>-0.154470</td>\n",
       "      <td>-0.143131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-12-31</th>\n",
       "      <td>-0.244307</td>\n",
       "      <td>-0.216753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-12-31</th>\n",
       "      <td>0.031203</td>\n",
       "      <td>0.031695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-12-31</th>\n",
       "      <td>-0.069878</td>\n",
       "      <td>-0.067493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-12-31</th>\n",
       "      <td>0.424412</td>\n",
       "      <td>0.528691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>0.089965</td>\n",
       "      <td>0.094136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-12-31</th>\n",
       "      <td>-0.131319</td>\n",
       "      <td>-0.123062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-31</th>\n",
       "      <td>0.063517</td>\n",
       "      <td>0.065578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>-0.282245</td>\n",
       "      <td>-0.245911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>0.201333</td>\n",
       "      <td>0.223032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>0.129858</td>\n",
       "      <td>0.138667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-12-31</th>\n",
       "      <td>0.046884</td>\n",
       "      <td>0.048001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-31</th>\n",
       "      <td>-0.163991</td>\n",
       "      <td>-0.151250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-31</th>\n",
       "      <td>-0.037709</td>\n",
       "      <td>-0.037007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>0.119264</td>\n",
       "      <td>0.126668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-12-31</th>\n",
       "      <td>0.140644</td>\n",
       "      <td>0.151014</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return\n",
       "Day                               \n",
       "1995-12-31   -0.154200   -0.142899\n",
       "1996-12-31    0.501639    0.651425\n",
       "1997-12-31    0.264019    0.302153\n",
       "1998-12-31   -0.040505   -0.039695\n",
       "1999-12-31    0.175423    0.191750\n",
       "2000-12-31    0.416917    0.517277\n",
       "2001-12-31   -0.230898   -0.206180\n",
       "2002-12-31   -0.192575   -0.175167\n",
       "2003-12-31    0.097735    0.102670\n",
       "2004-12-31   -0.167233   -0.153997\n",
       "2005-12-31   -0.086924   -0.083253\n",
       "2006-12-31    0.834792    1.304334\n",
       "2007-12-31    0.676302    0.966593\n",
       "2008-12-31   -1.061146   -0.653941\n",
       "2009-12-31    0.587690    0.799825\n",
       "2010-12-31   -0.154470   -0.143131\n",
       "2011-12-31   -0.244307   -0.216753\n",
       "2012-12-31    0.031203    0.031695\n",
       "2013-12-31   -0.069878   -0.067493\n",
       "2014-12-31    0.424412    0.528691\n",
       "2015-12-31    0.089965    0.094136\n",
       "2016-12-31   -0.131319   -0.123062\n",
       "2017-12-31    0.063517    0.065578\n",
       "2018-12-31   -0.282245   -0.245911\n",
       "2019-12-31    0.201333    0.223032\n",
       "2020-12-31    0.129858    0.138667\n",
       "2021-12-31    0.046884    0.048001\n",
       "2022-12-31   -0.163991   -0.151250\n",
       "2023-12-31   -0.037709   -0.037007\n",
       "2024-12-31    0.119264    0.126668\n",
       "2025-12-31    0.140644    0.151014"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "年末价格计算的收益率:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>555.2900</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-12-31</th>\n",
       "      <td>917.0200</td>\n",
       "      <td>555.2900</td>\n",
       "      <td>0.651425</td>\n",
       "      <td>0.501639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-12-31</th>\n",
       "      <td>1194.1000</td>\n",
       "      <td>917.0200</td>\n",
       "      <td>0.302153</td>\n",
       "      <td>0.264019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-12-31</th>\n",
       "      <td>1146.7000</td>\n",
       "      <td>1194.1000</td>\n",
       "      <td>-0.039695</td>\n",
       "      <td>-0.040505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1999-12-31</th>\n",
       "      <td>1366.5800</td>\n",
       "      <td>1146.7000</td>\n",
       "      <td>0.191750</td>\n",
       "      <td>0.175423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-12-31</th>\n",
       "      <td>2073.4800</td>\n",
       "      <td>1366.5800</td>\n",
       "      <td>0.517277</td>\n",
       "      <td>0.416917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-12-31</th>\n",
       "      <td>1645.9700</td>\n",
       "      <td>2073.4800</td>\n",
       "      <td>-0.206180</td>\n",
       "      <td>-0.230898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>1357.6500</td>\n",
       "      <td>1645.9700</td>\n",
       "      <td>-0.175167</td>\n",
       "      <td>-0.192575</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-12-31</th>\n",
       "      <td>1497.0400</td>\n",
       "      <td>1357.6500</td>\n",
       "      <td>0.102670</td>\n",
       "      <td>0.097735</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-12-31</th>\n",
       "      <td>1266.5000</td>\n",
       "      <td>1497.0400</td>\n",
       "      <td>-0.153997</td>\n",
       "      <td>-0.167233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-12-31</th>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1266.5000</td>\n",
       "      <td>-0.083253</td>\n",
       "      <td>-0.086924</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-12-31</th>\n",
       "      <td>2675.4700</td>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1.304334</td>\n",
       "      <td>0.834792</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-12-31</th>\n",
       "      <td>5261.5600</td>\n",
       "      <td>2675.4700</td>\n",
       "      <td>0.966593</td>\n",
       "      <td>0.676302</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-12-31</th>\n",
       "      <td>1820.8100</td>\n",
       "      <td>5261.5600</td>\n",
       "      <td>-0.653941</td>\n",
       "      <td>-1.061146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-12-31</th>\n",
       "      <td>3277.1400</td>\n",
       "      <td>1820.8100</td>\n",
       "      <td>0.799825</td>\n",
       "      <td>0.587690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-12-31</th>\n",
       "      <td>2808.0800</td>\n",
       "      <td>3277.1400</td>\n",
       "      <td>-0.143131</td>\n",
       "      <td>-0.154470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-12-31</th>\n",
       "      <td>2199.4200</td>\n",
       "      <td>2808.0800</td>\n",
       "      <td>-0.216753</td>\n",
       "      <td>-0.244307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-12-31</th>\n",
       "      <td>2269.1300</td>\n",
       "      <td>2199.4200</td>\n",
       "      <td>0.031695</td>\n",
       "      <td>0.031203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-12-31</th>\n",
       "      <td>2115.9800</td>\n",
       "      <td>2269.1300</td>\n",
       "      <td>-0.067493</td>\n",
       "      <td>-0.069878</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-12-31</th>\n",
       "      <td>3234.6800</td>\n",
       "      <td>2115.9800</td>\n",
       "      <td>0.528691</td>\n",
       "      <td>0.424412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>3539.1800</td>\n",
       "      <td>3234.6800</td>\n",
       "      <td>0.094136</td>\n",
       "      <td>0.089965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-12-31</th>\n",
       "      <td>3103.6400</td>\n",
       "      <td>3539.1800</td>\n",
       "      <td>-0.123062</td>\n",
       "      <td>-0.131319</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-31</th>\n",
       "      <td>3307.1700</td>\n",
       "      <td>3103.6400</td>\n",
       "      <td>0.065578</td>\n",
       "      <td>0.063517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>2493.9000</td>\n",
       "      <td>3307.1700</td>\n",
       "      <td>-0.245911</td>\n",
       "      <td>-0.282245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>3050.1200</td>\n",
       "      <td>2493.9000</td>\n",
       "      <td>0.223032</td>\n",
       "      <td>0.201333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>3473.0700</td>\n",
       "      <td>3050.1200</td>\n",
       "      <td>0.138667</td>\n",
       "      <td>0.129858</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-12-31</th>\n",
       "      <td>3639.7800</td>\n",
       "      <td>3473.0700</td>\n",
       "      <td>0.048001</td>\n",
       "      <td>0.046884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-31</th>\n",
       "      <td>3089.2579</td>\n",
       "      <td>3639.7800</td>\n",
       "      <td>-0.151251</td>\n",
       "      <td>-0.163992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-31</th>\n",
       "      <td>2974.9348</td>\n",
       "      <td>3089.2579</td>\n",
       "      <td>-0.037007</td>\n",
       "      <td>-0.037709</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>3351.7630</td>\n",
       "      <td>2974.9348</td>\n",
       "      <td>0.126668</td>\n",
       "      <td>0.119264</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-12-31</th>\n",
       "      <td>3857.9270</td>\n",
       "      <td>3351.7630</td>\n",
       "      <td>0.151014</td>\n",
       "      <td>0.140644</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose  Raw_return  Log_return\n",
       "Day                                                     \n",
       "1995-12-31   555.2900        NaN         NaN         NaN\n",
       "1996-12-31   917.0200   555.2900    0.651425    0.501639\n",
       "1997-12-31  1194.1000   917.0200    0.302153    0.264019\n",
       "1998-12-31  1146.7000  1194.1000   -0.039695   -0.040505\n",
       "1999-12-31  1366.5800  1146.7000    0.191750    0.175423\n",
       "2000-12-31  2073.4800  1366.5800    0.517277    0.416917\n",
       "2001-12-31  1645.9700  2073.4800   -0.206180   -0.230898\n",
       "2002-12-31  1357.6500  1645.9700   -0.175167   -0.192575\n",
       "2003-12-31  1497.0400  1357.6500    0.102670    0.097735\n",
       "2004-12-31  1266.5000  1497.0400   -0.153997   -0.167233\n",
       "2005-12-31  1161.0600  1266.5000   -0.083253   -0.086924\n",
       "2006-12-31  2675.4700  1161.0600    1.304334    0.834792\n",
       "2007-12-31  5261.5600  2675.4700    0.966593    0.676302\n",
       "2008-12-31  1820.8100  5261.5600   -0.653941   -1.061146\n",
       "2009-12-31  3277.1400  1820.8100    0.799825    0.587690\n",
       "2010-12-31  2808.0800  3277.1400   -0.143131   -0.154470\n",
       "2011-12-31  2199.4200  2808.0800   -0.216753   -0.244307\n",
       "2012-12-31  2269.1300  2199.4200    0.031695    0.031203\n",
       "2013-12-31  2115.9800  2269.1300   -0.067493   -0.069878\n",
       "2014-12-31  3234.6800  2115.9800    0.528691    0.424412\n",
       "2015-12-31  3539.1800  3234.6800    0.094136    0.089965\n",
       "2016-12-31  3103.6400  3539.1800   -0.123062   -0.131319\n",
       "2017-12-31  3307.1700  3103.6400    0.065578    0.063517\n",
       "2018-12-31  2493.9000  3307.1700   -0.245911   -0.282245\n",
       "2019-12-31  3050.1200  2493.9000    0.223032    0.201333\n",
       "2020-12-31  3473.0700  3050.1200    0.138667    0.129858\n",
       "2021-12-31  3639.7800  3473.0700    0.048001    0.046884\n",
       "2022-12-31  3089.2579  3639.7800   -0.151251   -0.163992\n",
       "2023-12-31  2974.9348  3089.2579   -0.037007   -0.037709\n",
       "2024-12-31  3351.7630  2974.9348    0.126668    0.119264\n",
       "2025-12-31  3857.9270  3351.7630    0.151014    0.140644"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "使用groupby计算的年度收益率:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995</th>\n",
       "      <td>-0.154200</td>\n",
       "      <td>-0.142899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996</th>\n",
       "      <td>0.501639</td>\n",
       "      <td>0.651425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997</th>\n",
       "      <td>0.264019</td>\n",
       "      <td>0.302153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998</th>\n",
       "      <td>-0.040505</td>\n",
       "      <td>-0.039695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1999</th>\n",
       "      <td>0.175423</td>\n",
       "      <td>0.191750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000</th>\n",
       "      <td>0.416917</td>\n",
       "      <td>0.517277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001</th>\n",
       "      <td>-0.230898</td>\n",
       "      <td>-0.206180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002</th>\n",
       "      <td>-0.192575</td>\n",
       "      <td>-0.175167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003</th>\n",
       "      <td>0.097735</td>\n",
       "      <td>0.102670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004</th>\n",
       "      <td>-0.167233</td>\n",
       "      <td>-0.153997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005</th>\n",
       "      <td>-0.086924</td>\n",
       "      <td>-0.083253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <td>0.834792</td>\n",
       "      <td>1.304334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007</th>\n",
       "      <td>0.676302</td>\n",
       "      <td>0.966593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <td>-1.061146</td>\n",
       "      <td>-0.653941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009</th>\n",
       "      <td>0.587690</td>\n",
       "      <td>0.799825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010</th>\n",
       "      <td>-0.154470</td>\n",
       "      <td>-0.143131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011</th>\n",
       "      <td>-0.244307</td>\n",
       "      <td>-0.216753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012</th>\n",
       "      <td>0.031203</td>\n",
       "      <td>0.031695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013</th>\n",
       "      <td>-0.069878</td>\n",
       "      <td>-0.067493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014</th>\n",
       "      <td>0.424412</td>\n",
       "      <td>0.528691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015</th>\n",
       "      <td>0.089965</td>\n",
       "      <td>0.094136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016</th>\n",
       "      <td>-0.131319</td>\n",
       "      <td>-0.123062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017</th>\n",
       "      <td>0.063517</td>\n",
       "      <td>0.065578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018</th>\n",
       "      <td>-0.282245</td>\n",
       "      <td>-0.245911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019</th>\n",
       "      <td>0.201333</td>\n",
       "      <td>0.223032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020</th>\n",
       "      <td>0.129858</td>\n",
       "      <td>0.138667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021</th>\n",
       "      <td>0.046884</td>\n",
       "      <td>0.048001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022</th>\n",
       "      <td>-0.163991</td>\n",
       "      <td>-0.151250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023</th>\n",
       "      <td>-0.037709</td>\n",
       "      <td>-0.037007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024</th>\n",
       "      <td>0.119264</td>\n",
       "      <td>0.126668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025</th>\n",
       "      <td>0.140644</td>\n",
       "      <td>0.151014</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Log_return  Raw_Return\n",
       "year                        \n",
       "1995   -0.154200   -0.142899\n",
       "1996    0.501639    0.651425\n",
       "1997    0.264019    0.302153\n",
       "1998   -0.040505   -0.039695\n",
       "1999    0.175423    0.191750\n",
       "2000    0.416917    0.517277\n",
       "2001   -0.230898   -0.206180\n",
       "2002   -0.192575   -0.175167\n",
       "2003    0.097735    0.102670\n",
       "2004   -0.167233   -0.153997\n",
       "2005   -0.086924   -0.083253\n",
       "2006    0.834792    1.304334\n",
       "2007    0.676302    0.966593\n",
       "2008   -1.061146   -0.653941\n",
       "2009    0.587690    0.799825\n",
       "2010   -0.154470   -0.143131\n",
       "2011   -0.244307   -0.216753\n",
       "2012    0.031203    0.031695\n",
       "2013   -0.069878   -0.067493\n",
       "2014    0.424412    0.528691\n",
       "2015    0.089965    0.094136\n",
       "2016   -0.131319   -0.123062\n",
       "2017    0.063517    0.065578\n",
       "2018   -0.282245   -0.245911\n",
       "2019    0.201333    0.223032\n",
       "2020    0.129858    0.138667\n",
       "2021    0.046884    0.048001\n",
       "2022   -0.163991   -0.151250\n",
       "2023   -0.037709   -0.037007\n",
       "2024    0.119264    0.126668\n",
       "2025    0.140644    0.151014"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算年度收益率\n",
    "# 方法1：使用resample函数的'YE'参数（年末）\n",
    "Year_data1 = data_new.resample('YE')['Log_return'].sum().to_frame(name='Log_return')\n",
    "Year_data1['Raw_Return'] = np.exp(Year_data1['Log_return']) - 1\n",
    "\n",
    "# 方法2：使用年末价格计算\n",
    "Year_data2 = data_new.resample('YE')['Close'].last().to_frame()\n",
    "Year_data2['Preclose'] = Year_data2['Close'].shift(1)\n",
    "Year_data2['Raw_return'] = Year_data2['Close'] / Year_data2['Preclose'] - 1\n",
    "Year_data2['Log_return'] = np.log(Year_data2['Close']) - np.log(Year_data2['Preclose'])\n",
    "\n",
    "# 方法3：使用groupby按年分组\n",
    "data_new4 = data_new.copy()\n",
    "data_new4['year'] = data_new4.index.year\n",
    "Year_data3 = data_new4.groupby('year')['Log_return'].sum().to_frame()\n",
    "Year_data3['Raw_Return'] = np.exp(Year_data3['Log_return']) - 1\n",
    "\n",
    "# 显示结果\n",
    "print(\"年度对数收益率汇总:\")\n",
    "Year_data1\n",
    "print(\"\\n年末价格计算的收益率:\")\n",
    "Year_data2\n",
    "print(\"\\n使用groupby计算的年度收益率:\")\n",
    "Year_data3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c228860",
   "metadata": {},
   "source": [
    "计算滚动收益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "cb307d45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "滚动收益率 (基于对数收益率累加):\n",
      "            Rolling_5d_Return  Rolling_10d_Return  Rolling_20d_Return  \\\n",
      "Day                                                                     \n",
      "2025-08-25           0.041720            0.064705            0.079386   \n",
      "2025-08-26           0.037854            0.055229            0.071660   \n",
      "2025-08-27           0.009065            0.031732            0.051064   \n",
      "2025-08-28           0.019225            0.048318            0.075671   \n",
      "2025-08-29           0.008408            0.043594            0.083702   \n",
      "\n",
      "            Rolling_30d_Return  Rolling_60d_Return  \n",
      "Day                                                 \n",
      "2025-08-25            0.103394            0.160143  \n",
      "2025-08-26            0.103676            0.150627  \n",
      "2025-08-27            0.084644            0.125628  \n",
      "2025-08-28            0.092916            0.135781  \n",
      "2025-08-29            0.091511            0.139592  \n",
      "\n",
      "滚动收益率 (基于价格变化):\n",
      "            Rolling_5d_Price_Return  Rolling_10d_Price_Return  \\\n",
      "Day                                                             \n",
      "2025-08-25                 0.041720                  0.064705   \n",
      "2025-08-26                 0.037854                  0.055229   \n",
      "2025-08-27                 0.009065                  0.031732   \n",
      "2025-08-28                 0.019225                  0.048318   \n",
      "2025-08-29                 0.008408                  0.043594   \n",
      "\n",
      "            Rolling_20d_Price_Return  Rolling_30d_Price_Return  \\\n",
      "Day                                                              \n",
      "2025-08-25                  0.079386                  0.103394   \n",
      "2025-08-26                  0.071660                  0.103676   \n",
      "2025-08-27                  0.051064                  0.084644   \n",
      "2025-08-28                  0.075671                  0.092916   \n",
      "2025-08-29                  0.083702                  0.091511   \n",
      "\n",
      "            Rolling_60d_Price_Return  \n",
      "Day                                   \n",
      "2025-08-25                  0.160143  \n",
      "2025-08-26                  0.150627  \n",
      "2025-08-27                  0.125628  \n",
      "2025-08-28                  0.135781  \n",
      "2025-08-29                  0.139592  \n"
     ]
    }
   ],
   "source": [
    "# 计算滚动收益率（例如：过去30天、60天、90天的收益率 注意这里指的是前30个观测值）\n",
    "# 这在金融分析中非常常见，用于观察不同时间窗口的收益表现\n",
    "\n",
    "# 方法1：使用rolling窗口函数计算滚动对数收益率之和\n",
    "rolling_returns = pd.DataFrame()\n",
    "for window in [5, 10, 20, 30, 60]:\n",
    "    # 计算滚动窗口的对数收益率之和\n",
    "    rolling_log_return = data_new['Log_return'].rolling(window=window).sum()\n",
    "    # 转换为原始收益率\n",
    "    rolling_returns[f'Rolling_{window}d_Return'] = np.exp(rolling_log_return) - 1\n",
    "\n",
    "# 方法2：使用pct_change计算滚动价格变化\n",
    "rolling_price_returns = pd.DataFrame()\n",
    "for window in [5, 10, 20, 30, 60]:\n",
    "    rolling_price_returns[f'Rolling_{window}d_Price_Return'] = data_new['Close'].pct_change(periods=window)\n",
    "\n",
    "# 显示结果\n",
    "print(\"滚动收益率 (基于对数收益率累加):\")\n",
    "print(rolling_returns.tail())\n",
    "print(\"\\n滚动收益率 (基于价格变化):\")\n",
    "print(rolling_price_returns.tail())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "57f24c0a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不同方法计算的累积收益率:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Cumulative_Log_Return</th>\n",
       "      <th>Cumulative_Return</th>\n",
       "      <th>Cumulative_Return_Prod</th>\n",
       "      <th>Cumulative_Return_Alt</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>-0.012409</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>0.009127</td>\n",
       "      <td>0.009169</td>\n",
       "      <td>0.009169</td>\n",
       "      <td>0.009169</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>-0.001514</td>\n",
       "      <td>-0.001513</td>\n",
       "      <td>-0.001513</td>\n",
       "      <td>-0.001513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>-0.011035</td>\n",
       "      <td>-0.010974</td>\n",
       "      <td>-0.010974</td>\n",
       "      <td>-0.010974</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>-0.034340</td>\n",
       "      <td>-0.033757</td>\n",
       "      <td>-0.033757</td>\n",
       "      <td>-0.033757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>1.790819</td>\n",
       "      <td>4.994361</td>\n",
       "      <td>4.994361</td>\n",
       "      <td>4.994361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>1.786903</td>\n",
       "      <td>4.970931</td>\n",
       "      <td>4.970931</td>\n",
       "      <td>4.970931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>1.769160</td>\n",
       "      <td>4.865922</td>\n",
       "      <td>4.865922</td>\n",
       "      <td>4.865922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>1.780475</td>\n",
       "      <td>4.932675</td>\n",
       "      <td>4.932675</td>\n",
       "      <td>4.932675</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>1.784196</td>\n",
       "      <td>4.954793</td>\n",
       "      <td>4.954793</td>\n",
       "      <td>4.954793</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7445 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Cumulative_Log_Return  Cumulative_Return  Cumulative_Return_Prod  \\\n",
       "Day                                                                            \n",
       "1995-01-03              -0.012409          -0.012333               -0.012333   \n",
       "1995-01-04               0.009127           0.009169                0.009169   \n",
       "1995-01-05              -0.001514          -0.001513               -0.001513   \n",
       "1995-01-06              -0.011035          -0.010974               -0.010974   \n",
       "1995-01-09              -0.034340          -0.033757               -0.033757   \n",
       "...                           ...                ...                     ...   \n",
       "2025-08-25               1.790819           4.994361                4.994361   \n",
       "2025-08-26               1.786903           4.970931                4.970931   \n",
       "2025-08-27               1.769160           4.865922                4.865922   \n",
       "2025-08-28               1.780475           4.932675                4.932675   \n",
       "2025-08-29               1.784196           4.954793                4.954793   \n",
       "\n",
       "            Cumulative_Return_Alt  \n",
       "Day                                \n",
       "1995-01-03              -0.012333  \n",
       "1995-01-04               0.009169  \n",
       "1995-01-05              -0.001513  \n",
       "1995-01-06              -0.010974  \n",
       "1995-01-09              -0.033757  \n",
       "...                           ...  \n",
       "2025-08-25               4.994361  \n",
       "2025-08-26               4.970931  \n",
       "2025-08-27               4.865922  \n",
       "2025-08-28               4.932675  \n",
       "2025-08-29               4.954793  \n",
       "\n",
       "[7445 rows x 4 columns]"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算累积收益率\n",
    "# 累积收益率用于观察长期投资表现，从某个起始点开始累积\n",
    "\n",
    "# 方法1：使用对数收益率累加后转换\n",
    "# 这是最准确的方法，特别是对于长期累积\n",
    "cumulative_returns = pd.DataFrame()\n",
    "cumulative_returns['Cumulative_Log_Return'] = data_new['Log_return'].cumsum()\n",
    "cumulative_returns['Cumulative_Return'] = np.exp(cumulative_returns['Cumulative_Log_Return']) - 1\n",
    "\n",
    "# 方法2：使用cumprod函数直接累乘(1+r)\n",
    "# 这种方法在金融实践中也很常见\n",
    "cumulative_returns['Cumulative_Return_Prod'] = (1 + data_new['Raw_return']).cumprod() - 1\n",
    "\n",
    "# 方法3：使用pandas的累积函数\n",
    "cumulative_returns['Cumulative_Return_Alt'] = data_new['Raw_return'].add(1).cumprod().sub(1)\n",
    "\n",
    "# 显示结果\n",
    "print(\"不同方法计算的累积收益率:\")\n",
    "cumulative_returns"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py313",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
